﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameForKids
{

    public class ChartScreen : DrawableGameComponent
    {
        public event EventHandler Clicked = delegate { };

        //texture cua bang
        protected Texture2D boardImg;
        protected Texture2D teachImg;
        
        // texture hinh giao vien
        protected Rectangle rectOfBoard;
        protected Rectangle rectOfTeach;
        
       //ham kiem tra hien hoac an
        public bool show { get; set; }
        public bool hideComplete = false;

        //ve do thi
        //truc x
        Line lineX;
        // truc y
        Line lineY;
        //cac truc x trong do thi
        Line lineX2;
        Line lineX4;
        Line lineX6;
        Line lineX8;
        Line lineX10;
        // diem dau va diem cuoi truc x
        Vector2 vectorLineXStart;
        Vector2 vectorLineXEnd;
        // diem dau va diem cuoi truc y
        Vector2 vectorLineYStart;
        Vector2 vectorLineYEnd;
        SpriteFont font;
        int numOfList; 
        int Xstart;
        public static IList<DataScore> listDataScore;
        Line line;

        public void ShowChart()
        {
            show = true;
        }

        public void HideChart()
        {
            show = false;
        }
        
        // vi tri luc an 
        private Point locationOfBoard = new Point(-800, 0);
        private Point locationOfTeach = new Point(817, 282);
        public static int levelOfGame = 1;

        public ChartScreen(Game game): base(game)
        {

            font = this.Game.Content.Load<SpriteFont>("Fonts/HelpFont");
            boardImg = this.Game.Content.Load<Texture2D>("Textures/Backgrounds/board");
            teachImg = this.Game.Content.Load<Texture2D>("Textures/Backgrounds/teacher");
            rectOfBoard = boardImg.Bounds;
            rectOfBoard.X = locationOfBoard.X;
            rectOfBoard.Y = locationOfBoard.Y;
            rectOfTeach = teachImg.Bounds;
            rectOfTeach.X = locationOfTeach.X;
            rectOfTeach.Y = locationOfTeach.Y;
            show = false;

            base.Initialize();

            line = new Line(GraphicsDevice, Color.Orange);
            line.setThickness(4);

            lineX = new Line(GraphicsDevice, Color.White);
            lineX2 = new Line(GraphicsDevice, Color.Wheat);
            lineX4 = new Line(GraphicsDevice, Color.Wheat);
            lineX6 = new Line(GraphicsDevice, Color.Wheat);
            lineX8 = new Line(GraphicsDevice, Color.Wheat);
            lineX10 = new Line(GraphicsDevice, Color.Wheat);
            lineX2.setThickness(2);
            lineX4.setThickness(2);
            lineX6.setThickness(2);
            lineX8.setThickness(2);
            lineX10.setThickness(2);
            lineY = new Line(GraphicsDevice, Color.White);
            vectorLineXStart = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 350);
            vectorLineXEnd = new Vector2(rectOfBoard.X + 680, rectOfBoard.Y + 350);
            vectorLineYStart = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 350);
            vectorLineYEnd = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 50);
        }


        private void UpdateLine()
        {
            vectorLineXStart = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 350);
            vectorLineXEnd = new Vector2(rectOfBoard.X + 680, rectOfBoard.Y + 350);
            vectorLineYStart = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 350);
            vectorLineYEnd = new Vector2(rectOfBoard.X + 80, rectOfBoard.Y + 50);
        }

        public bool Contains(Point position)
        {
            return rectOfBoard.Contains(position);
        }


        public bool Contains(Vector2 position)
        {
            return Contains(new Point((int)position.X, (int)position.Y));
        }

        private void drawChart(SpriteBatch spriteBatch)
        {
           
            if (listDataScore.Count == 0)
            {
                if(GameForKids.language == GameForKids.VIET)
                     spriteBatch.DrawString(font, "[ chưa có dữ liệu ]",
                        new Vector2((rectOfBoard.Width - font.MeasureString("[ chưa có dữ liệu ]").X)/2 + rectOfBoard.X, 
                            (rectOfBoard.Height - font.MeasureString("").Y)/2), 
                        Color.White);
                else
                    spriteBatch.DrawString(font, "[ Data empty ]",
                        new Vector2((rectOfBoard.Width - font.MeasureString("[ Data empty ]").X) / 2 + rectOfBoard.X,
                            (rectOfBoard.Height - font.MeasureString("").Y) / 2),
                        Color.White);

            }
            else
            {
                if (GameForKids.language == GameForKids.VIET && levelOfGame>0)
                    spriteBatch.DrawString(font,
                        "Bảng điểm màn " + levelOfGame.ToString(),
                        new Vector2(rectOfBoard.X + 90, rectOfBoard.Y + 320),
                        Color.White);
                else if (GameForKids.language == GameForKids.ANH && levelOfGame > 0)
                    spriteBatch.DrawString(font,
                        "Scores of level " + levelOfGame.ToString(),
                        new Vector2(rectOfBoard.X + 90, rectOfBoard.Y + 320),
                        Color.White);

                UpdateLine();
                lineX.DrawLine(spriteBatch, vectorLineXStart, vectorLineXEnd);
                lineY.DrawLine(spriteBatch, vectorLineYStart, vectorLineYEnd);
                lineX2.DrawLine(spriteBatch, vectorLineXStart - new Vector2(0, 60), vectorLineXEnd - new Vector2(0, 60));
                lineX4.DrawLine(spriteBatch, vectorLineXStart - new Vector2(0, 120), vectorLineXEnd - new Vector2(0, 120));
                lineX6.DrawLine(spriteBatch, vectorLineXStart - new Vector2(0, 180), vectorLineXEnd - new Vector2(0, 180));
                lineX8.DrawLine(spriteBatch, vectorLineXStart - new Vector2(0, 240), vectorLineXEnd - new Vector2(0, 240));
                lineX10.DrawLine(spriteBatch, vectorLineXStart - new Vector2(0, 300), vectorLineXEnd - new Vector2(0, 300));

                spriteBatch.DrawString(font, "0", new Vector2(rectOfBoard.X + 50, rectOfBoard.Y + 330), Color.White);
                spriteBatch.DrawString(font, "2", new Vector2(rectOfBoard.X + 50, rectOfBoard.Y + 270), Color.White);
                spriteBatch.DrawString(font, "4", new Vector2(rectOfBoard.X + 50, rectOfBoard.Y + 210), Color.White);
                spriteBatch.DrawString(font, "6", new Vector2(rectOfBoard.X + 50, rectOfBoard.Y + 150), Color.White);
                spriteBatch.DrawString(font, "8", new Vector2(rectOfBoard.X + 50, rectOfBoard.Y + 90), Color.White);
                spriteBatch.DrawString(font, "10", new Vector2(rectOfBoard.X + 40, rectOfBoard.Y + 30), Color.White);

                if (numOfList <= 10)
                {
                    if (numOfList == 1)
                    {
                        //ve duong thu nhat

                        line.DrawLine(spriteBatch, new Vector2(80 + rectOfBoard.X, 350),
                                        new Vector2(Xstart + 80 + rectOfBoard.X, 350 - (30 * listDataScore[0].score)));
                        //ve ngay
                        spriteBatch.DrawString(font, listDataScore[0].day, new Vector2(Xstart + 80 + rectOfBoard.X, 360),
                            Color.White, MathHelper.Pi / 2, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
                    }
                    else
                    {
                        //ve cac duong va ngay con lai
                        for (int i = 1; i < numOfList; i++)
                        {
                            line.DrawLine(spriteBatch, new Vector2((i - 1) * Xstart + 80 + rectOfBoard.X, 350 - (30 * listDataScore[i - 1].score)),
                                new Vector2(i * Xstart + 80 + rectOfBoard.X, 350 - (30 * listDataScore[i].score)));

                            spriteBatch.DrawString(font, listDataScore[i].day, new Vector2(i * Xstart + 90 + rectOfBoard.X, 360),
                                 Color.White, MathHelper.Pi / 2, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
                        }
                        spriteBatch.DrawString(font, listDataScore[0].day, new Vector2(90 + rectOfBoard.X, 360),
                                 Color.White, MathHelper.Pi / 2, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
                    }

                }
                else
                {  
                    for (int i = 10; i > 1; i--)
                    {
                        line.DrawLine(spriteBatch, new Vector2((10 - i) * Xstart + 80 + rectOfBoard.X, 350 - (30 * listDataScore[numOfList - i].score)),
                                 new Vector2((11 - i) * Xstart + 80 + rectOfBoard.X, 350 - (30 * listDataScore[numOfList - i + 1].score)));

                        spriteBatch.DrawString(font, listDataScore[numOfList - i].day, new Vector2((10 - i) * Xstart + 90 + rectOfBoard.X, 360),
                                 Color.White, MathHelper.Pi / 2, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
                    }
                    spriteBatch.DrawString(font, listDataScore[numOfList - 1].day, new Vector2((10 - 1) * Xstart + 90 + rectOfBoard.X, 360),
                                 Color.White, MathHelper.Pi / 2, new Vector2(0, 0), 0.6f, SpriteEffects.None, 0);
                }
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {

            if (show)
            {
                numOfList = listDataScore.Count;
                if (numOfList < 10 && numOfList >0)
                {
                    if (numOfList == 1)
                        Xstart = 500;
                    else
                       Xstart = 500 / (numOfList-1);
                }
                else
                    Xstart = 56;
                if (rectOfBoard.X >= 0)
                {
                    // hieu ung ve teacher
                    if (rectOfTeach.X > 607)
                    {
                        rectOfTeach.X -= 30;
                        spriteBatch.Draw(teachImg, rectOfTeach, Color.White);
                    }
                    spriteBatch.Draw(boardImg, rectOfBoard, Color.White);
                    drawChart(spriteBatch);
                    spriteBatch.Draw(teachImg, rectOfTeach, Color.White);
                }
                else
                {
                    // hieu ung hien bang diem
                    rectOfBoard.X += 40;
                    spriteBatch.Draw(boardImg, rectOfBoard, Color.White);
                    drawChart(spriteBatch);
                }
            }
            else
            {
                if (rectOfTeach.X < 817)
                {
                    // hieu ung an teacher
                    rectOfTeach.X += 30;
                    spriteBatch.Draw(teachImg, rectOfTeach, Color.White);
                }
                if (rectOfBoard.X > -800)
                {
                    // hieu ung an bang diem
                    rectOfBoard.X -= 40;
                    spriteBatch.Draw(boardImg, rectOfBoard, Color.White);
                    drawChart(spriteBatch);
                }
            }
            //spriteBatch.DrawString(font, "Moved: " + GamePlayMemory.moved.ToString(), new Vector2(10, 100), Color.Black);
            //spriteBatch.DrawString(font,"Number score: "+ listDataScore.Count.ToString(), new Vector2(10, 150), Color.Azure);
        }

        public void TouchEvent(TouchLocation touch)
        {
            switch (touch.State)
            {
                case TouchLocationState.Pressed:
                    Pressed(touch);
                    break;
            }
        }

        public void Pressed(TouchLocation touch)
        {
            if (!show)
                return;
            if (Contains(touch.Position))
            {
                Clicked(this, new EventArgs());
            }
        }
    }
}
